package com.maimang.platform.config;

import com.maimang.platform.common.config.Global;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Rocky
 * @version 1.0.0
 * @classname SwaggerConfig
 * @description Swagger 2 接口配置
 * @date 2019-04-23 17:02
 * @email jintaozhao@qq.com
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

	private String title = "平台数据接口服务中心";

	/**
	 * 创建api
	 *
	 * @return
	 */
	// @Bean
	public Docket createBaseApi() {
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(apiInfo())
				.groupName("系统接口")
				.select()
				// 扫描所有@Api注解类下的@ApiOperation注解方法
				.apis(RequestHandlerSelectors.basePackage("com.maimang.platform.controller"))
				.paths(PathSelectors.any())
				.build();
	}

	/**
	 * 添加集成JWT API文档
	 * @return
	 */
	@Bean
	public Docket createRestApi() {
		ParameterBuilder tokenPar = new ParameterBuilder();
		List<Parameter> pars = new ArrayList<>();
		//header中的token参数非必填，传空也可以
		tokenPar.name("token").description("请求接口所需Token，参数非必填，传空也可以")
				.modelRef(new ModelRef("string")).parameterType("header")
				.required(false).build();
		pars.add(tokenPar.build());
		return new Docket(DocumentationType.SWAGGER_12)
				.apiInfo(apiInfo())
				.groupName("引领性指标")
				.select()
				.apis(RequestHandlerSelectors.basePackage("com.maimang.platform.crm"))
				.paths(PathSelectors.any())
				.build()
				.globalOperationParameters(pars);
	}

	/**
	 * platform-activiti接口说明
	 *
	 * @return
	 */
	// @Bean
	public Docket createActivitiApi() {
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(apiInfo())
				.groupName("工作流程")
				.select()
				// 扫描activiti包下@Api注解类下的@ApiOperation注解方法
				.apis(RequestHandlerSelectors.basePackage("com.maimang.platform.activiti"))
				.paths(PathSelectors.any())
				.build();
	}

	/**
	 * platform-quartz接口说明
	 *
	 * @return
	 */
	// @Bean
	public Docket createQuartzApi() {
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(apiInfo())
				.groupName("定时任务")
				.select()
				// 扫描quartz包下@Api注解类下的@ApiOperation注解方法
				.apis(RequestHandlerSelectors.basePackage("com.maimang.platform.quartz"))
				.paths(PathSelectors.any())
				.build();
	}

	/**
	 * platform-oa接口说明
	 *
	 * @return
	 */
	// @Bean
	public Docket createOaApi() {
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(apiInfo())
				.groupName("在线办公")
				.select()
				// 扫描oa包下@Api注解类下的@ApiOperation注解方法
				.apis(RequestHandlerSelectors.basePackage("com.maimang.platform.oa"))
				.paths(PathSelectors.any())
				.build();
	}

	/**
	 * platform-crm接口说明
	 *
	 * @return
	 */
	// @Bean
	public Docket createCrmApi() {
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(apiInfo())
				.groupName("客户关系")
				.select()
				// 扫描crm包下@Api注解类下的@ApiOperation注解方法
				.apis(RequestHandlerSelectors.basePackage("com.maimang.platform.crm"))
				.paths(PathSelectors.any())
				.build();
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				.title(title)
				.description(title)
				.contact(new Contact(Global.getName(), Global.getWebsite(), Global.getEmail()))
				.contact(new Contact(Global.getCompanyName(), Global.getWebsite(), Global.getEmail()))
				.version("版本号:" + Global.getVersion())
				.build();
	}
}